package org.zbclan.bf2.rcon.ui;

import java.util.ArrayList;
import java.util.List;

import org.vaadin.artur.icepush.ICEPush;
import org.zbclan.bf2.rcon.BF2RconAppRunner;
import org.zbclan.bf2.rcon.event.DisplayEvent;
import org.zbclan.bf2.rcon.event.ListLocalProfilesEvent;
import org.zbclan.bf2.rcon.event.PushEvent;
import org.zbclan.bf2.rcon.event.RunAppletEvent;

import com.google.gwt.event.shared.HandlerRegistration;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Window.CloseListener;

public class MainWindow extends Window implements CloseListener,
		PushEvent.Handler, DisplayEvent.Handler,
		ListLocalProfilesEvent.Handler, RunAppletEvent.Handler {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3194575780931124912L;

	private final List<HandlerRegistration> externalEventRegistration;

	private final BF2RconAppRunner appRunner;

	private final CssLayout content;
	private final ICEPush pusher;

	// private final BF2RconApplet applet;
	// private final NewWindowButton popup;

	public MainWindow(BF2RconAppRunner appRunner) {

		this.appRunner = appRunner;

		externalEventRegistration = new ArrayList<HandlerRegistration>();

		content = new CssLayout();
		// content.setSizeFull();

		pusher = new ICEPush();
		// pusher.addStyleName("icepush");

		// applet = new BF2RconApplet(appRunner);
		// popup = new NewWindowButton();

		CssLayout container = new CssLayout();
		container.addComponent(content);
		container.addComponent(pusher);
		// container.addComponent(applet);
		// container.addComponent(popup);
		// container.setSizeFull();

		setCaption("BF2 Remote Console (vaadin)");
		setContent(container);
		addListener((CloseListener) this);

		externalEventRegistration.add(appRunner.addHandler(
				PushEvent.Handler.TYPE, this));
		externalEventRegistration.add(appRunner.addHandler(
				DisplayEvent.Handler.TYPE, this));
		externalEventRegistration.add(appRunner.addHandler(
				RunAppletEvent.Handler.TYPE, this));

		externalEventRegistration.add(appRunner.addHandler(
				ListLocalProfilesEvent.Handler.TYPE, this));

		appRunner.fireEvent(new DisplayEvent(new PlayerServerSelectComponent(
				appRunner)));
		// appRunner
		// .fireEvent(new DisplayEvent(new StatisticsComponent(appRunner)));

		return;
	}

	@Override
	public void windowClose(CloseEvent e) {
		for (HandlerRegistration r : externalEventRegistration) {
			r.removeHandler();
		}
	}

	@Override
	public void onEvent(PushEvent event) {
		pusher.push();
	}

	@Override
	public void onEvent(DisplayEvent event) {
		content.removeAllComponents();
		content.addComponent(event.getComponent());
	}

	@Override
	public void onEvent(ListLocalProfilesEvent event) {

		LocalProfileSelectComponent c = new LocalProfileSelectComponent(
				appRunner);
		appRunner.fireEvent(new DisplayEvent(c));
		c.display(event.getProfiles());

		return;
	}

	@Override
	public void onEvent(RunAppletEvent event) {
		// applet.display(event.getCommand());
		// popup.display(appRunner.getContext() + "/applet/?"
		// + StringUtils.join(event.getParams(), "&"));
	}
}
